import Vue from "vue";
import VueRouter from "vue-router";
import ElementUI from "element-ui";
import "element-ui/lib/theme-chalk/index.css";

Vue.use(ElementUI);
Vue.use(VueRouter);
//登录/首页/
import Login from "@/Base/Login.vue";
import Main from "@/Base/Main.vue";

//基本资料
import Warehouse from "@/BS/bs_Warehouse.vue"; //仓库
import Unit from "@/BS/Unit.vue"; //计量单位
import bs_client from "@/BS/bs_client.vue"; //客户列表
import bs_vendor from "@/BS/bs_vendor.vue"; //供应商列表
import Employee from "@/BS/bs_Employee.vue"; //员工
import bs_model from "@/BS/bs_model.vue"; //产品类型
import Home from "@/Base/home.vue"; //工作台
import sale_SaleOrder from "@/Sale/sale_SaleOrder.vue"; //销售订单
import sale_SaleOut from "@/Sale/sale_SaleOut.vue"; //销售出库
import sale_SaleReturn from "@/Sale/sale_SaleReturn.vue"; //销售退货
import bs_attribute from "@/BS/bs_attribute.vue"; //属性
import bs_goods from "@/BS/bs_goods.vue"; //物品
import bs_Warning from "@/BS/bs_Warning.vue"; //库存预警
import inv_Inventory from "@/Inv/inv_Inventory.vue"; //物品库存
import inv_Storage from "@/Inv/inv_Storage.vue"; //其他物品入库
import inv_OutStorehouse from "@/Inv/inv_OutStorehouse.vue"; //其他物品出库
import inv_Count from "@/Inv/inv_Count.vue"; //库存盘点

//采购管理
import pur_Purchase from "@/PurKernel/pur_Purchase.vue"; //采购订单
import pur_Storage from "@/PurKernel/pur_Storage.vue"; //采购订单
import pur_Zales_Return from "@/PurKernel/pur_Zales_Return.vue"; //采购退货

//生产管理
import pro_orders from '@/ProCtion/pro_orders.vue'//生产订单
import pro_WareHousing from '@/ProCtion/pro_WareHousing.vue'//生产订单入库

//权限管理
import User from "@/Quan/User.vue"; //用户管理
import RolesQ from "@/Quan/RolesQ.vue"; //角色权限管理

import axios from "axios";
const router = new VueRouter({
  mode: "history",
  routes: [
    {
      name: "Login",
      path: "/",
      component: Login,
    },
    {
      name: "zhuye",
      path: "/Main",
      component: Main,
      meta: { requiresAuth: true }, // 添加需要验证登录状态
      children: [
        {
          name: "Warehouse",
          path: "/bs_Warehouse",
          component: Warehouse,
        },
        {
          name: "Unit",
          path: "/Unit",
          component: Unit,
        },
        {
          name: "bs_client",
          path: "/bs_client",
          component: bs_client,
        },
        {
          name: "bs_vendor",
          path: "/bs_vendor",
          component: bs_vendor,
        },
        {
          name: "Employee",
          path: "/bs_Employee",
          component: Employee,
        },
        {
          name: "Home",
          path: "",
          component: Home,
        },
        {
          name: "sale_SaleOrder",
          path: "/sale_SaleOrder",
          component: sale_SaleOrder,
        },
        {
          name: "sale_SaleOut",
          path: "/sale_SaleOut",
          component: sale_SaleOut,
        },
        {
          name: "sale_SaleReturn",
          path: "/sale_SaleReturn",
          component: sale_SaleReturn,
        },
        {
          name: "bs_attribute",
          path: "/bs_attribute",
          component: bs_attribute,
        },
        {
          name: "bs_goods",
          path: "/bs_goods",
          component: bs_goods,
        },
        {
          //库存预警
          name: "bs_Warning",
          path: "/bs_Warning",
          component: bs_Warning,
        },
        {
          //物品库存
          name: "inv_Inventory",
          path: "/inv_Inventory",
          component: inv_Inventory,
        },
        {
          //其他物品入库
          name: "inv_Storage",
          path: "/inv_Storage",
          component: inv_Storage,
        },
        {
          //其他物品出库
          name: "inv_OutStorehouse",
          path: "/inv_OutStorehouse",
          component: inv_OutStorehouse,
        },
        {
          //库存盘点
          name: "inv_Count",
          path: "/inv_Count",
          component: inv_Count,
        },
        {
          name: "pur_Purchase",
          path: "/pur_Purchase",
          component: pur_Purchase,
        },
        {
          name: "pur_Storage",
          path: "/pur_Storage",
          component: pur_Storage,
        },
        {
          name: "pur_Zales_Return",
          path: "/pur_Zales_Return",
          component: pur_Zales_Return,
        },
        {
          //产品类型
          name: "bs_model",
          path: "/bs_model",
          component: bs_model,
        },
        {
          //生产订单
          name: "pro_orders",
          path: "/pro_orders",
          component: pro_orders,
        },
        {
          //生产入库订单
          name: "pro_WareHousing",
          path: "/pro_WareHousing",
          component: pro_WareHousing,
        },
        {
          //用户管理
          name: "User",
          path: "/User",
          component: User,
        },
        {
          //角色权限管理
          name: "RolesQ",
          path: "/RolesQ",
          component: RolesQ,
        },
      ],
    },
  ],
});

// 使用Vue Router中的导航守卫来检查用户是否已登录，若未登录则重定向到登录页面

router.beforeEach((to, from, next) => {
  if (to.meta.requiresAuth) {
    if (localStorage.getItem("token")) {
      next();
    } else {
      next({ path: "/" }); // 用户未登录，重定向到登录页面
    }
  } else {
    next(); // 无需验证的路由直接放行
  }
});

axios.interceptors.request.use((config) => {
  const token = localStorage.getItem("token");
  if (token) {
    config.headers["Authorization"] = `Bearer ${token}`;
  }
  return config;
});
export default router;
